package com.practise.rabbitmqdemo.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 * @Author : JCccc
 * @CreateTime : 2019/9/3
 * @Description :
 **/
@Configuration
public class DirectRabbitConfig {

    public static final String ROUTING_1 ="routing1";
    public static final String ROUTING_11 ="routing11";
    public static final String ROUTING_2 ="routing2";
    public static final String EXCHANGE_1 ="exchange1";
    public static final String EXCHANGE_2 ="exchange2";

    public static final String LONELY_DIRECT_EXCHANGE ="lonelyDirectExchange";

    public static final String QUEUE_1 ="queue1";
    public static final String QUEUE_11 ="queue11";
    public static final String QUEUE_2 ="queue2";

    public static final String LONELY_QUEUE ="lonelyQueue";

    /**
     * 队列  queue1
     * @return
     */
    @Bean
    public Queue getQueue1() {
        // durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
        // exclusive:默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
        //   return new Queue("TestDirectQueue",true,true,false);
        //一般设置一下队列的持久化就好,其余两个就是默认false
        return new Queue(QUEUE_1,true);
    }

    /**
     * Direct交换机 exchange1
     * @return
     */
    @Bean
    DirectExchange getExchange1() {
        return new DirectExchange(EXCHANGE_1,true,false);
    }

    /**
     * 将队列和交换机绑定, 并设置用于匹配键：routing1
     * @return
     */
    @Bean
    Binding bindingDirect1() {
        return BindingBuilder.bind(getQueue1()).to(getExchange1()).with(ROUTING_1);
    }

    @Bean
    Queue getQueue2() {
        return new Queue(QUEUE_2);
    }

    @Bean
    DirectExchange getExchange2() {
        return new DirectExchange(EXCHANGE_2);
    }

    @Bean
    Binding bindingDirect2() {
        return BindingBuilder.bind(getQueue2()).to(getExchange2()).with(ROUTING_2);
    }



    @Bean
    Queue getQueue11() {
        return new Queue(QUEUE_11);
    }

    /**
     * 1个交换机绑定多个queue
     * @return
     */
    @Bean
    Binding bindingDirect11() {
        return BindingBuilder.bind(getQueue11()).to(getExchange1()).with(ROUTING_11);
    }

    @Bean
    DirectExchange lonelyDirectExchange() {
        return new DirectExchange(LONELY_DIRECT_EXCHANGE);
    }
    @Bean
    Queue lonelyQueue() {
        return new Queue(LONELY_QUEUE);
    }
 
 
 
}